package com.gujia.atomic.bank;

import java.util.concurrent.atomic.AtomicLong;

public class AtomicBank {

    private AtomicLong balance = null;

    public AtomicBank(int money) {
        this.balance = new AtomicLong(money);
        System.out.println("总账" + money);
    }


    public void getMoney(int money){
        Long curMoney = balance.get();

        if (curMoney>0) {
            try {
                Thread.sleep(10);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            if (this.balance.compareAndSet(curMoney, curMoney - money)){
                System.out.println(Thread.currentThread().getName() + "->" + curMoney);
            }


        }
    }
}
